<?php
	require_once 'models/models_chitietnhap.php';
	$mod = $_GET['mod'];
	$obctnhap = new models_chitietnhap();
	require_once 'models/models_ncc.php';
	$obncc = new models_ncc();
	require_once 'models/models_mathang.php';
	$obMh = new  models_mathang();
	$p=new pagerss();//khoi tao doi tuong pager
	switch ($mod){
		case 'ctnhap':{
			$limit=7;
			$count=$obctnhap->tongsodong();
			$start=$p->findstart($limit);
			$page=$p->findpages($count,$limit);
			$data['list'] = $obctnhap->danhsachctn($start, $limit);
			$pagelist=$p->pagelist($_GET['page'],$page);
			$data['listncc'] = $obncc->_listncc();
			$data['listmathang'] = $obMh->listMatHang();
			require 'views/admin/chitietnhap/index.php';
			break;
		}
		case 'add_ctnhap':{
			if($_POST){
				$mh=$obMh->getMatHang($_POST['cmb_mathang']);
				if(isset($mh))
				{
					if($mh['SoLuong']==0)
					{
						$SoLuong=$_POST['soluong'];
						$obMh->updatemh($SoLuong,$_POST['cmb_mathang']);
					}else{
						$SoLuong=$_POST['soluong']+$mh['SoLuong'];
						$obMh->updatemh($SoLuong,$_POST['cmb_mathang']);
						
					}
				}
				$obctnhap->_addCTNhap($_POST['cmb_ncc'], $_POST['cmb_mathang'], $_POST['dongia'], $_POST['soluong'], date('Y-m-d'));
				echo '<script>window.location.href="admin.php?mod=ctnhap"</script>';
			}
			$listncc['list'] = $obctnhap->_getncc();
			$listmathang['list'] = $obctnhap->_getMatHang();
			require 'views/admin/chitietnhap/add.php';
			break;
		}
		case 'del_ctnhap':{
			$ctn=$obctnhap->_getCTNhap($_GET['id']);
			$mh=$obMh->getMatHang($ctn['MaMH']);
			if($mh['SoLuong']>$ctn['SoLuongNhap'])
			{
				$SoLuong=$mh['SoLuong']-$ctn['SoLuongNhap'];
				$obMh->updatemh($SoLuong,$mh['MaMH']);
				$obctnhap->_delCTNhap($_GET['id']);
				echo '<script>window.location.href="admin.php?mod=ctnhap"</script>';
			}else 
			echo '<script>window.location.href="admin.php?mod=ctnhap"</script>';
			
			
			break;
		}
		case 'edit_ctnhap':{
			$ctn = $obctnhap->_getCTNhap($_GET['id']);
			$data['listncc'] = $obncc->_listncc();
			$data['listmathang'] = $obMh->listMatHang();
			if($_POST){
			$ctn = $obctnhap->_getCTNhap($_GET['id']);
			$mh=$obMh->getMatHang($_POST['cmb_mathang']);
			if($mh['SoLuong']==0){
				$SoLuong=$_POST['soluong'];
				$obMh->updatemh($SoLuong,$_POST['cmb_mathang']);				
			}else{
				$SoLuong=$mh['SoLuong']-$ctn['SoLuongNhap']+$_POST['soluong'];
				$obMh->updatemh($SoLuong,$_POST['cmb_mathang']);	
			}
			
				$obctnhap->updateCTNhap($_POST['cmb_ncc'],$_POST['cmb_mathang'],$_POST['dongia'],$_POST['soluong'],$_GET['id']);
				echo '<script>window.location.href="admin.php?mod=ctnhap"</script>';
			}
			
			require 'views/admin/chitietnhap/edit.php';
			break;
		}
	   case 'timchitietnhap':{
		if($_POST){
		   
			$data['listmathang'] = $obMh->_find($_POST['ctn']);
			
			
		}
			 $data['ctn'] = $obctnhap->_listCTNhap();
			$data['listncc'] = $obncc->_listncc();
			include_once 'views/admin/chitietnhap/timchitietnhap.php';
			break;
		
	}
	}
?>